﻿package view.components.portfolio 
{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import model.utils.ModuleConfig;
	import org.casalib.display.CasaMovieClip;
	import view.components.utils.UIEvent;
	import view.module.PortfolioMediator;
	
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author Lobanov
	 */
	public class Thumbnails extends CasaMovieClip
	{		
		private var _containerThumbnails:CasaMovieClip;
		
		public function Thumbnails() 
		{			
			_containerThumbnails = new CasaMovieClip();
			addChild(_containerThumbnails);
			
			this.y = 300;
			this.x = 0;
		}
		
		public function initThumbnails($xmlData:XMLList):void 
		{				
			var defX:Number = 0;
			var thumbData:XML = getAsset($xmlData, PortfolioMediator.MOD_NAME);
			var inc:int = 0;
			
			for each (var item:XML in thumbData.lastwork.asset) {
				var itemThumb:ItemThumbnails = new ItemThumbnails(ModuleConfig.DOMAIN+item.@image, item.@id );
				itemThumb.addEventListener( ItemThumbnails.OVER_DEPTH, onOverThumb );
				_containerThumbnails.addChild( itemThumb );	
				
				//set position
				itemThumb.x += defX; 
				defX += itemThumb.width + 1;
				inc++;
				//trace(inc);
			}
			
			function onOverThumb(e:UIEvent = null):void {
				_containerThumbnails.setChildIndex( e.data as ItemThumbnails, 
						_containerThumbnails.numChildren -1
				);
			}
			
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
		
		private function mouseMoveHandler(e:MouseEvent):void {
			var differenceWidth:Number = stage.stageWidth - _containerThumbnails.width;
			var targetPositionX:Number = stage.mouseX * (differenceWidth / stage.stageWidth);
			TweenLite.to(_containerThumbnails, 1, { x:targetPositionX } );
		}
		
		
		private function getAsset($xmlData, $modName):XML 
		{
			for each (var item:XML in $xmlData.page) {
				if (item.@mod == $modName) 
					return item;
			}
		}
		
		public function setPosition($width:Number, $height:Number):void
		{
			this.x = $width / 2;
			this.y = $height / 2;
		}
	}

}